class Container:
    def __init__(self):
        self.__head__=[]
        self.__body__=[]
        self.__tail__=[]
        self.__type__="Container"
    def output(self,f):
        for i in self.__head__:
            i.output(f)
        for i in self.__body__:
            i.output(f)
        for i in self.__tail__:
            i.output(f)
    def getContent(self):
        return self.__body__
    def __str__(self):
        return "container"
    def breif(self):
        return "container: \n"
    def show(self):
        s=self.brief()
        s=s+"----------------------\n"
        content=self.getContent()
        for i in range(len(content)):
            s=s+"    #"+`i`+" "+content[i].brief()
        s=s+"\nend\n======================"    
        return s

class Text:
    def __init__(self,text):
        self.__content__=text
    def output(self,f):
        f.write(self.__content__)
    def __str__(self):
        return self.__content__
    def brief(self):
        return self.__content__+"\n"    

class Tag(Text):
    def __init__(self,tag,attr={}):
        self.__content__="\n<"+tag
        for i in attr:
            self.__content__=self.__content__+' %s="%s"'%(i,attr[i])
        self.__content__=self.__content__+">"

class EndTag(Text):
    def __init__(self,tag):
        self.__content__="</"+tag+">\n"

class EmptyBlock(Text):
    def __init__(self,tag,attr={}):
        self.__content__="<"+tag
        for i in attr:
            self.__content__=self.__content__+' %s="%s"'%(i,attr[i])
        self.__content__=self.__content__+" />\n"

                
class Block(Container):
    def __init__(self,tag,content=None,attr={}):
        Container.__init__(self)
        self.__head__.append(Tag(tag,attr))
        self.__tail__.append(EndTag(tag))
        self.__type__=tag
        if(content):
            self.__body__.append(content)
    def addContent(self,content):
        self.__body__.append(content)
    def __str__(self):
        s=""
        for i in self.__head__:
            s=s+str(i)
        for i in self.__body__:
            s=s+str(i)
        for i in self.__tail__:
            s=s+str(i)
        return s
    def brief(self):
        if self.getContent():
            return self.__type__+": "+self.getContent()[0].brief()
        else:
            return self.__type__+": (empty)\n"

        
class Slide(Block):
    def __init__(self):
        Block.__init__(self,"div",None,{"class":"slide"})
        self.__title__=""
        self.__type__="slide"
    def addTitle(self,title):
        self.addContent(Title(title))
        self.__title__=title
    def addImg(self,filename):
        self.addContent(Img(filename))
    def getTitle(self):
        return self.__title__
    def brief(self):
        if self.__title__:
            return self.__type__+": "+self.__title__+"\n"
        else:
            return Block.brief(self)

class Img(EmptyBlock):
    def __init__(self,filename):
        Emptyblock.__init__(self,"img",{"src":"img/"+filename})
            
class Title(Block):
    def __init__(self,title):
        Block.__init__(self,"h1",Text(title))
        self.__type__="title"
            
class List(Block):
    def __init__(self):
        Block.__init__(self,"ul")
        self.__type__="list"

class Point(Block):
    def __init__(self,text):
        Block.__init__(self,"li",Text(text))
        self.__type__="point"        
        
class Presentation(Container):
    DOCTYPE="""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">"""
    XMLNS="http://www.w3.org/1999/xhtml"
    def __init__(self,title,date,presenter,affiliation):
        Container.__init__(self)
        self.__head__.append(Text(Presentation.DOCTYPE))
        self.__head__.append(Tag("html",{"xmlns":Presentation.XMLNS}))
        self.__htmlhead__=Block("head")
        self.__htmlbody__=Block("body")
        self.__layout__=Block("div",None,{"class":"layout"})
        self.__slides__=Block("div",None,{"class":"presentation"})
        self.__frontpage__=Slide()
        self.__endpage__=Slide()
        self.__body__.append(self.__htmlhead__)
        self.__body__.append(self.__htmlbody__)
        self.__htmlbody__.addContent(self.__layout__)
        self.__htmlbody__.addContent(self.__slides__)
        self.__slides__.addContent(self.__frontpage__)
        self.__slides__.addContent(self.__endpage__)
        self.__tail__.append(EndTag("html"))
        self.addTitle(title)
        self.addMeta({"name":"generator","content":"S5"})
        self.addMeta({"name":"version","content":"S5 1.1"})
        self.addMeta({"name":"defaultView","content":"slideshow"})
        self.addMeta({"name":"controlVis","content":"hidden"})
        self.addLink({"rel":"stylesheet","href":"ui/default/slides.css","type":"text/css","media":"projection","id":"slideProj"})
        self.addLink({"rel":"stylesheet","href":"ui/default/outline.css","type":"text/css","media":"screen","id":"outlineStyle"})
        self.addLink({"rel":"stylesheet","href":"ui/default/print.css","type":"text/css","media":"print","id":"slidePrint"})
        self.addLink({"rel":"stylesheet","href":"ui/default/opera.css","type":"text/css","media":"projection","id":"operaFix"})
        self.addScript({"src":"ui/default/slides.js","type":"text/javascript"})
        self.addLayout(Block("div",Text("<!--Do Not Edit-->"),{"id":"controls"}))
        self.addLayout(Block("div",Text("<!--Do Not Edit-->"),{"id":"currentSlide"}))
        self.addLayout(Block("div",None,{"id":"header"}))
        footer=Block("div",None,{"id":"footer"})
        footer.addContent(Block("h1",Text(date)))
        footer.addContent(Block("h2",Text(title)))
        self.addLayout(footer)
        
        # Generating FrontPage
        self.__frontpage__.addTitle(title)
        self.__frontpage__.addContent(Block("h3",Text(presenter)))
        self.__frontpage__.addContent(Block("h4",Text(affiliation)))

        #Generating EndPage
        self.__endpage__.addTitle("Thanks for your attention!")        
    def addMeta(self,attr):
        self.__htmlhead__.addContent(EmptyBlock("meta",attr))    
    def addLink(self,attr):
        self.__htmlhead__.addContent(EmptyBlock("link",attr))
    def addScript(self,attr):
        self.__htmlhead__.addContent(Block("script",None,attr))
    def addTitle(self,title):
        self.__htmlhead__.addContent(Title(title))
    def addLayout(self,block):
        self.__layout__.addContent(block)
    def addSlide(self,slide):
        self.__slides__.__body__.insert(-1,slide)
    def getContent(self):
        return self.__slides__.getContent()
    def __str__(self):
        s="presentation:\n"
        slides=self.getContent()
        for i in range(len(slides)):
            s=s+"slide #"+`i`+":"+slides[i].brief()+"\n"
        return s
    def brief(self):
        return "presentation:\n"



        



         
                







        



        

        

